# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from aitaotu.items import AitaotuItem

domain = 'https://www.aitaotu.com'


class AitaotuSpider(scrapy.Spider):
    name = 'AiTaoTu'
    allowed_domains = ['aitaotu.com']
    start_urls = ['https://www.aitaotu.com/tag/tukmo.html']

    def parse(self, response):
        # 分类列表
        category_urls = response.xpath('//ul[@class="spnavc"]/li/a/@href').extract()
        for category in category_urls:
            catagory_url = domain + category
            request = scrapy.Request(url=catagory_url, callback=self.parse_category)
            yield request

    # 处理分类
    def parse_category(self, response):

        images_url = response.xpath('//ul[@id="mainbodypul"]/li/a/@href').extract()
        for url in images_url:
            image_url = domain + url
            request = scrapy.Request(url=image_url, callback=self.parse_images)
            yield request

    # 处理图片页面
    def parse_images(self, response):
        item = AitaotuItem()
        title1 = response.xpath('//div[@id="photos"]/h1/text()').extract()
        title2 = response.xpath('//div[@id="photos"]/h2/text()').extract()
        description = response.xpath('//div[@class="tsmaincont-main-cont-desc"]/h3/text()').extract()

        title1 = title1[0] if len(title1) else ''
        title2 = title2[0] if len(title2) else ''
        description = description[0] if len(description) else ''
        image_srcs = response.xpath('//p[@align="center"]/a/img/@src').extract()
        for src in image_srcs:
            item['category'] = title1
            item['image_url'] = src
            item['title'] = title2
            item['description'] = description
            item['local_url'] = title1 + '/' + src.split('/')[-1]
            yield item

        next_page = response.xpath('//a[text()="下一页"]/@href').extract()
        if next_page:
            url = domain + next_page[0]
            yield Request(url, callback=self.parse_images)
        else:
            print('%s - 没有下一页啦～～～' % title1)
        pass
